package com.cxkj.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.cxkj.dao.MeentryMapper;
import com.cxkj.dao.SummaryMapper;
import com.cxkj.dto.Meentry;
import com.cxkj.dto.Summary;
import com.cxkj.dto.vo.SumVo;
import com.cxkj.service.SummaryService;
import com.cxkj.tools.Page;
@Service("summaryService")
public class SummaryServiceImpl implements SummaryService{
	
	@Autowired
	private SummaryMapper summaryMapper;
	@Autowired
	private MeentryMapper meentryMapper;
	/**
	 * 新增单据信息
	 * 新增商户需录入具体册号、单据号
	 */
	public void insertSum(Summary summary) {
		//册号
		Integer chuce=Integer.valueOf(summary.getSvolume());
		//结束册号
		Integer jiece = Integer.valueOf(summary.getScolumn1());
		//初始单据
		Integer chudan = Integer.valueOf(summary.getSinitiate());
		//结束单据
		Integer jiedan = Integer.valueOf(summary.getSfinish());
		Summary sum = new Summary();
		Meentry me = new Meentry();
		Integer chud = chudan;
		for (int i = chuce; i <= jiece; i++) {
			int k = 0;
			for (int j = chud; j < i*(chudan+25); j++) {
				chud = j;
				k++;
				if(k==1){
					//存入单据汇总表
					sum.setSvolume(String.valueOf(i));
					sum.setSinitiate(String.valueOf(j));
					sum.setSfinish(String.valueOf(j+24));
					sum.setSdatastate("未领取");
					sum.setStime(new Date());
					sum.setSstatus("1");
					sum.setScreatetime(new Date());
					
					int add = summaryMapper.insertSelective(sum);
					System.out.println("Summary新增"+add+"条数据");
				}
				//存入商户录入表
				me.setMvolume(String.valueOf(i));
				me.setMdocumentnum(String.valueOf(j));
				me.setMaccstatus("0");//结算状态：0未结算、1已结算
				me.setMstatus("1");
				me.setMcreatetime(new Date());
				int addMe = meentryMapper.insertSelective(me);
				System.out.println("Meentry新增第"+k+"条数据");
				if(k==25){
					break;
				}
			}
			chud = chud+1;
			if(i==jiece){
				break;
			}
		}
		
	}
	/**
	 * 分页查询数据
	 */
	public List<Summary> showSum(HttpServletRequest request, Model model) {
		String pageNow = request.getParameter("pageNow");
		Page page = null;
		List<Summary> sumList = new ArrayList<Summary>();
		int count = (int)summaryMapper.getCount();
		if(pageNow != null){
			page = new Page(count, Integer.valueOf(pageNow));
			sumList = summaryMapper.showSumByCount(page.getStartPos(), page.getPageSize());
		}else{
			page = new Page(count, 1);
			sumList = summaryMapper.showSumByCount(page.getStartPos(), page.getPageSize());
		}
		
		SumVo sumVo = new SumVo();
		//共多少册
		sumVo.setSumV(count);
		//查询已领取多少册
		sumVo.setSumQ((int)summaryMapper.selectByQu());
		sumVo.setSumW(count-(int)summaryMapper.selectByQu());
		
		model.addAttribute("page", page);
		model.addAttribute("sumVo", sumVo);
		return sumList;
	}
	
	
	public List<Meentry> sumTest(Summary summary) {
		//册号
		Integer chuce=Integer.valueOf(summary.getSvolume());
		//结束册号
		Integer jiece = Integer.valueOf(summary.getScolumn1());
		List<Meentry> list = null;
		for (int i = chuce; i <= jiece; i++) {
			list = meentryMapper.meenTest(String.valueOf(i));
			if(list.size() != 0){
				break;
			}
			if(i==jiece){
				break;
			}
		}
		
		return list;
	}
	
	
	
	
	

}
